package com.five.years.common.utils;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

public class IdGenUtils {

	private static final char[] CHS = "ABCDEFGHJKLMNPQRSTUVWXYZ1234567890".toCharArray();
	/**
	 * 封装JDK自带的UUID, 通过Random数字生成, 中间无-分割.
	 */
	public static String uuid() {
		return UUID.randomUUID().toString().replaceAll("-", "");
	}

	public static void main(String[] args) {
		System.out.println(IdGenUtils.uuid());
		System.out.println(IdGenUtils.uuid());
	}


	/*获取随机数字*/
	public  static String getRandomNumber(int length) {
		String chars = "012356789";
		char[] rands = new char[length];
		for (int i = 0; i < length; i++) {
			int rand = (int) (Math.random() * 9);
			rands[i] = chars.charAt(rand);
		}
		return new String(rands);
	}

	/*获取随机字符 去除字母O，I 数字1，0*/
	public  static String getRandomCharacter(int length) {
		String chars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
		char[] rands = new char[length];
		for (int i = 0; i < length; i++) {
			rands[i] = chars.charAt(new Random().nextInt(32));
		}
		return new String(rands);
	}

	public  static String getRandomLowerCharacter(int length) {
		String chars = "abcdefghijklmnpqrestuvwxyz123456789";
		char[] rands = new char[length];
		for (int i = 0; i < length; i++) {
			rands[i] = chars.charAt(new Random().nextInt(32));
		}
		return new String(rands);
	}



	/**
	 * @throws
	 * @Title: generateSmsCode
	 * @Description: 生成随机短信验证码
	 * @param: length 短信验证码长度
	 * @param: @return
	 * @return: String
	 * @author FrankWong
	 */
	public synchronized static String generateCheckPass(int length) {
		String chars = "012356789";
		char[] rands = new char[length];
		for (int i = 0; i < length; i++) {
			int rand = (int) (Math.random() * 9);
			rands[i] = chars.charAt(rand);
		}
		return new String(rands);
	}

	/**
	 * 生成订单Id
	 * @param userId
	 * @return
	 */
	public static String genOrderId(int userId) {
		return new SimpleDateFormat("yyyyMMddHHmmssS").format(new Date()) + generateCheckPass(3);
	}

	public static String numericCode(int len) {
		if (len < 4) {
			len = 4;
		}
		if (len > 64) {
			len = 64;
		}
		char[] charPoll = new char[(len/10 + 2) * 10];
		for (int i = 0;i < charPoll.length;i++) {
			charPoll[i] = (char) (i % 10 + 48);
		}
		char[] ret = new char[len];
		int pollMax = charPoll.length;
		Random random = new Random();
		for (int i = 0;i < len;i++) {
			int index = random.nextInt(pollMax);
			ret[i] = charPoll[index];
			pollMax--;
			if (index != pollMax) {
				charPoll[index] = charPoll[pollMax];
			}
		}
		return new String(ret);
	}

	/**
	 * @Author jiangfeng.zhu
	 * @Description 按日期生成系统流水号
	 * @Date 16:53 2020/11/3
	 **/
	public static String getSystemNo() {
		//格式：T+yyyymmddHHmiss+6位流水
		Date dNow = new Date();
		StringBuffer sbuffer = new StringBuffer();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		sbuffer.append(sdf.format(dNow));
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
		String incKey = "TN" + dateFormat.format(dNow);
		Long no = RedisUtils.increment(incKey, 1);
		String sysTemp = "00000000"+no;
		if(no == 1){
			//清理上一天的key
			Calendar calendar = Calendar.getInstance();   //得到日历
			calendar.setTime(dNow);//把当前时间赋给日历
			calendar.add(Calendar.DAY_OF_MONTH, -1);  //设置为前一天
			Date dBefore = calendar.getTime();   //得到前一天的时间
			String lastIncKey = "TN" + dateFormat.format(dBefore);
			RedisUtils.remove(lastIncKey);
		}
		sbuffer.append(sysTemp.substring(sysTemp.length()-8));
		return sbuffer.toString();
	}

}
